<template>
	<!-- navbar -->
	<CpNavBar>
		<template v-slot:center>
			<up-tabs :list="list" :scrollable="false" v-model:current="current" :activeStyle="{
				color: '#303133',
				fontWeight: 'bold',
				transform: 'scale(1.05)'
			}" :inactiveStyle="{
				color: '#606266',
				transform: 'scale(1)'
			}">
			</up-tabs>
		</template>
	</CpNavBar>
	<!-- content -->
	<view class="post">
		<template v-for="post in PostData" :key="post._id">
			<PostItem :post="post"></PostItem>
		</template>
	</view>
	<view class="">
		<button @click="nextPage">下一条</button>
	</view>
	<!-- tabbar -->
	<CpTabBar></CpTabBar>
</template>

<script setup>
	import PostItem from '@/components/PostItem.vue'
	import { useTabbarStore, useUserStore } from '@/store/index.js'
	import { ref } from 'vue'
	import { FileUrl } from '@/utils/FileUrl.js'
	const userStore = useUserStore()
	// navbar
	const current = ref(1)
	const list = ref([
		{ name: '关注', badge: { isDot: true } },
		{ name: '推荐' },
		{ name: '电影' }
	])
	// content
	const PostData = ref([])
	
	let currentPage = 1
	const nextPage = () => {
		currentPage++
		getPostData()
	}
	
	// 获取帖子数据
	const getPostData = async () => {
		const res = await uniCloud.callFunction({
			name: 'get-post',
			data: {
				page: currentPage, // 页数
				pageSize: 2, // 条数
				userOpenid: false
			}
		})
		// 图片转临时链接
		const data = res.result.data
		const convertUrl = await Promise.all(data.map(async item => {
			item.userInfo = await userStore.getUserInfo(item.opneid)
			item.images = await FileUrl(item.images)
			const res = await uniCloud.getTempFileURL({
				fileList: [item.userInfo.avatarUrl]
			})
			item.userInfo.avatarUrl = res.fileList[0]
			return item
		}))
		// 合并数据
		PostData.value = [...PostData.value, ...data]
	}
	getPostData()

	// tabbar
	const tabbarStore = useTabbarStore()
	tabbarStore.setActive(0)
	uni.hideTabBar()
</script>
